package com.sky.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 创建filter过滤器的步骤：
 * 1.创建一个类实现Filter接口并实现接口中的方法：
 * 2.在web.xml注册
 * 
 * @author Administrator
 *
 */

public class EncodingFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		//把ServletRequest转化成我们处理的HttpServletRequest
		  HttpServletRequest request1=(HttpServletRequest)request;
		  HttpServletResponse response1=(HttpServletResponse)response;
		  request1.setCharacterEncoding("utf-8");
		  response1.setContentType("text/html;charset=utf-8");
		  //放行(所有的servlet访问都需要先经过EncodingFilter，他处理后放行后servlet才可以执行)
		  chain.doFilter(request1, response1);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	

}
